/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 */
package com.apache.order.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.order.entity.TOrder;
import com.apache.order.manager.TOrderManager;
import com.apache.tools.StrUtil;

/**
 * description:  订单列表查询
 * @author Hou Dayu 创建时间：2015-8-3  
 */
public class OrderListPluginImpl implements PluginConnector {

	private TOrderManager tOrderManager;

	/**
	 * TODO 订单列表查询.  
	 * @see com.dwusoft.api.manager.PluginConnector#execute(com.dwusoft.api.vo.ParamsVo)  
	 */
	public Object execute(ParamsVo vo) throws Exception {

		TOrder tOrderMain = setMethodParams(vo);
		ParamsVo<TOrder> pvo = new ParamsVo<TOrder>();
		pvo.setObj(tOrderMain);
		String pageIndex = Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "0");
		String pageSize = Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "0");
		ResultEntity entity = new ResultEntity();
		pvo.setParams("orderby", "created_time desc");
		String channels = String.valueOf(vo.getParams("channels"));
		if (StrUtil.isNotNull(channels)) {
			String str[] = channels.split(",");
			String strs = "";
			for (int i = 0; i < str.length; i++) {
				if (i == str.length - 1) {
					strs += "'" + str[i] + "'";
				} else {
					strs += "'" + str[i] + "',";
				}
			}
			pvo.setParams("channels", strs);
		}

		String sqlByKey = String.valueOf(vo.getParams("sqlByKey"));
		if (Validator.isNotNull(sqlByKey)) {
			pvo.setMethodKey(sqlByKey);
		}
		String orderStatuses = String.valueOf(vo.getParams("orderStatuses"));
		if (Validator.isNotNull(orderStatuses)) {
			pvo.setParams("orderStatuses", orderStatuses);
		}
		if ("0".equals(pageIndex) || "0".equals(pageSize)) {
			entity.setEntity(tOrderManager.getList(pvo));
		} else {
			pvo.setParams("pageIndex", pageIndex);
			pvo.setParams("pageSize", pageSize);
			entity.setEntity(tOrderManager.getPageInfo(pvo));
		}
		entity.setResult("查询成功");
		entity.setResult("true");
		return entity;
	}

	private TOrder setMethodParams(ParamsVo vo) {
		TOrder tOrderMain = new TOrder();
		tOrderMain.setOrderId(getParamStr(vo, "orderId"));
		tOrderMain.setBuyUserEname(getParamStr(vo, "buyUserEname"));
		tOrderMain.setSellUserEname(getParamStr(vo, "sellUserEname"));
		tOrderMain.setOrderStatus(getParamStr(vo, "orderStatus"));
		tOrderMain.setPayStatus(getParamStr(vo, "payStatus"));
		tOrderMain.setOrderType(getParamStr(vo, "orderType"));
		tOrderMain.setPayMethod(getParamStr(vo, "payMethod"));
		tOrderMain.setTransferType(getParamStr(vo, "transferType"));
		return tOrderMain;
	}

	private String getParamStr(ParamsVo vo, String attrName) {
		return Validator.getDefaultStr(String.valueOf(vo.getParams(attrName)), "");
	}

	public void settOrderManager(TOrderManager tOrderManager) {
		this.tOrderManager = tOrderManager;
	}

}
